package com.hailiao.beidou.message.ui.view

import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.graphics.painter.Painter

/**
 * 旋转图片
 * @Author d10ng
 * @Date 2023/11/22 15:11
 */

@Composable
fun RotatingImage(
    modifier: Modifier = Modifier,
    painter: Painter,
    rotating: Boolean
) {
    if (rotating) {
        val animateTween by rememberInfiniteTransition(label = "RotationAnimation").animateFloat(
            initialValue = 0f,
            targetValue = 360f,
            animationSpec = infiniteRepeatable(
                tween(1000, easing = LinearEasing), RepeatMode.Restart),
            label = "RotationTween"
        )

        Image(
            painter = painter,
            contentDescription = null,
            modifier = modifier.rotate(animateTween)
        )
    } else {
        Image(
            painter = painter,
            contentDescription = null,
            modifier = modifier
        )
    }
}